﻿using System;
using System.Net.Http;
using IdentityModel.Client;

namespace IdentityServerThirdPartDemo
{
    class Program
    {
        static void Main(string[] args)
        {

            var disc = DiscoveryClient.GetAsync("http://localhost:5000").ConfigureAwait(false).GetAwaiter().GetResult();
            if (disc.IsError)
            {
                Console.WriteLine(disc.Error);
            }


            var tokenClient = new TokenClient(disc.TokenEndpoint, "client", "secret");
            var tokenResurce = tokenClient.RequestClientCredentialsAsync("api").Result;

            if (tokenResurce.IsError)
            {
                Console.WriteLine(tokenResurce.Error);
            }
            else
            {
                Console.WriteLine(tokenResurce.Json);
            }

            var httpClient = new HttpClient();
            httpClient.SetBearerToken(tokenResurce.AccessToken);

            var response = httpClient.GetAsync("http://localhost:5001/api/values").Result;
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
            }

            Console.WriteLine("Hello World!");
        }
    }
}
